<?php
/**
 * ZView
 *
 * Zend Frameworks View Component for TYPO3
 *
 * @package    ZView
 * @copyright  Copyright (c) Zephir AG, www.zephir.ch
 */

/** Abstract class for extension */
require_once 'Zend/View/Helper/Abstract.php';

/**
 * Helper to generate a "link" element
 *
 * @package ZView
 * @copyright  Copyright (c) Zephir AG, www.zephir.ch
 */
class Tx_Zview_Helper_T3link extends Zend_View_Helper_Abstract
{

	/**
	 * The TYPO3 plugin
	 *
	 * @var tslib_pibase
	 */
	private $_pi;

	/**
	 * Generates a link to the current page (the caller page) using PI vars.
	 * This is the shortform of link()->thisPagePIvars().
	 *
	 * If no arguments are passed, returns a Tx_Zview_Helper_T3link object.
	 *
	 * @param string $str
	 * @param array $overrulePIvars
	 * @param boolean $cache
	 * @param boolean $clearAnyway
	 * @param integer $altPageId
	 * @return string|Tx_Zview_Helper_T3link
	 */
	public function t3link($str='', array $overrulePIvars=array(), $cache=true, $clearAnyway=false, $altPageId=0)
	{
        if (0 == func_num_args()) {
            return $this;
        }
		return $this->thisPagePIvars($str, $overrulePIvars, $cache, $clearAnyway, $altPageId);
	}

	/**
	 * Generates a link to a certain page.
	 *
	 * @param string $str The label between the a-tags
	 * @param integer $id The ID of the target page
	 * @param array $urlParameters
	 * @param string $target The value for the target attribute
	 * @return string The generated link tag
	 */
	public function toPage($str, $id, array $urlParameters=array(), $target='')
	{
		return $this->_pi->pi_linkToPage($str, $id, $target, $urlParameters);
	}

	/**
	 * Generates a link to a certain page using PI vars.
	 *
	 * @param string $str
	 * @param integer $id
	 * @param array $overrulePIvars
	 * @param boolean $cache Using cache or not (1=cached,0=no caching)
	 * @param boolean $clearAnyway
	 * @return string The generated link tag
	 */
	public function toPagePIvars($str, $id, array $overrulePIvars=array(), $cache=true, $clearAnyway=false)
	{
		return $this->_pi->pi_linkTP_keepPIvars($str, $overrulePIvars, $cache, $clearAnyway, $id);
	}

	/**
	 * Generates a link to the current page (the caller page).
	 *
	 * @param string $str
	 * @param array $urlParameters
	 * @param boolean $cache
	 * @param integer $altPageId
	 * @return string The generated link tag
	 */
	public function thisPage($str, array $urlParameters=array(), $cache=true, $altPageId=0)
	{
		return $this->_pi->pi_linkTP($str, $urlParameters, $cache, $altPageId);
	}

	/**
	 * Generates a link to the current page (the caller page) using PI vars.
	 *
	 * @param string $str
	 * @param array $overrulePIvars
	 * @param boolean $cache
	 * @param boolean $clearAnyway
	 * @param integer $altPageId
	 * @return string The generated link tag
	 */
	public function thisPagePIvars($str, array $overrulePIvars=array(), $cache=true, $clearAnyway=false, $altPageId=0)
	{
		return $this->_pi->pi_linkTP_keepPIvars($str, $overrulePIvars, $cache, $clearAnyway, $altPageId);
	}

    /**
     * Set the View object
     *
     * @param  Zend_View_Interface $view
     * @return Zend_View_Helper_Abstract
     */
	public function setView(Zend_View_Interface $view)
	{
		$this->view = $view;
		$this->_pi = $view->getPi();
		if(empty($this->_pi) || (!$this->_pi instanceof tslib_pibase)) {
			throw new Exception('Please set tslib_pibase using $view->setPi($this) in your plugin class.');
		}
        return $this;
	}

}

if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/zview/helper/T3link.php'])	{
	include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/zview/helper/T3link.php']);
}
